package com.louis.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

import com.louis.annotation.Json;
import com.louis.dao.GeneralDao;
import com.louis.util.JodaUtil;

/**
 * <p>
 * DomainObject
 * </p>
 * 
 * @author
 * @DateTime Jul 26, 2011 9:10:03 AM
 * @version 1.0.0
 */

@MappedSuperclass
@Json(transientField = false, nullField = false)
@Configurable(autowire = Autowire.BY_NAME)
public class DomainObject implements Serializable {

	private static final long		serialVersionUID	= 3531893584644895799L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int						id;

	@Version
	private int						version;
	@Column(name = "createtime", columnDefinition = "datetime")
	@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
	private DateTime				createTime			= JodaUtil.now();

	@Column(name = "updatetime", columnDefinition = "datetime")
	@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
	private DateTime				updateTime;

	private transient GeneralDao	generalDao;

	public DomainObject() {
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public DateTime getCreateTime() {
		return createTime;
	}

	public void setCreateTime(DateTime createTime) {
		this.createTime = createTime;
	}

	public DateTime getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(DateTime updateTime) {
		this.updateTime = updateTime;
	}

	/**
	 * @return the generalDao
	 */
	public GeneralDao getGeneralDao() {
		return this.generalDao;
	}

	/**
	 * @param generalDao
	 *            the generalDao to set
	 */
	@Autowired(required = true)
	public void setGeneralDao(GeneralDao generalDao) {
		this.generalDao = generalDao;
	}

	@Override
	public String toString() {
		return "DomainObject [createTime=" + createTime + ", id=" + id + ", updateTime=" + updateTime + ", version=" + version + "]";
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (this.getClass() != obj.getClass()) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		DomainObject that = (DomainObject) obj;
		return this.getId() == that.getId();
	}

	@Override
	public int hashCode() {
		return getId();
	}
}
